package com.lagou.validator;

import com.lagou.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @Authther: mrwu
 * @date:2021/3/23-03
 * @Description:com.lagou.validator
 */
@Component
public class Uservalidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        //设置要验证的类型
        return User.class.isAssignableFrom(aClass);
    }

    //编写验证逻辑
    @Override
    public void validate(Object o, Errors errors) {
       User user = (User) o;
       if (((User) o).getName().equals("zhangsan")){
           errors.rejectValue("name", "wuquanxian");
       }

       //可以使用validations工具类
//        ValidationUtils.rejectIfEmpty(errors, "name", "name 为空");
    }
}
